﻿using HiSport.Application.Service.Transaction.TransactionConfig.Service;
using HiSport.Application.Service.Order.OrderSiteService.Service;
using HiSport.Core.Entity.Pay;
using Essensoft.Paylink.WeChatPay.V3;
using Essensoft.Paylink.WeChatPay;
using Essensoft.Paylink.WeChatPay.V3.Notify;
using HiSport.Core.Entity.Order;

namespace HiSport.Application.Service.Transaction.Pay.Service
{

    [ApiDescriptionSettings("payManagement", Tag = "payment", SplitCamelCase = false)]
    public class PayNotifyService : ITransient, IPayNotifyService, IDynamicApiController
    {

        private readonly IWeChatPayNotifyClient _client;
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly ITransactionConfigService _transactionConfigService;
        private readonly IOrderSiteService _orderSiteService;
        private readonly SqlSugarRepository<PayRecord> _payRecord;
        /// <summary>
        /// 订单主表
        /// </summary>
        private readonly SqlSugarRepository<Core.Entity.Order.OrderSite> _order;
        /// <summary>
        /// 订单子表
        /// </summary>
        private readonly SqlSugarRepository<OrderSiteInfo> _orderInfo;
        public PayNotifyService(IHttpContextAccessor httpContextAccessor,
            IWeChatPayNotifyClient client,
            ITransactionConfigService transactionConfigService,
            SqlSugarRepository<PayRecord> payRecord,
            SqlSugarRepository<Core.Entity.Order.OrderSite> order,
            SqlSugarRepository<OrderSiteInfo> orderSiteInfo,
            IOrderSiteService orderSiteService)
        {
            _client = client;
            _httpContextAccessor = httpContextAccessor;
            _transactionConfigService = transactionConfigService;
            _orderSiteService = orderSiteService;
            _order = order;
            _orderInfo = orderSiteInfo;
            _payRecord = payRecord;
        }

        /// <summary>
        /// 支付结果通知
        /// </summary>
        [ApiDescriptionSettings(Name = "WeChatPayNotity"), HttpPost, AllowAnonymous]
        [DisplayName("微信支付结果通知")]
        [NonUnify]
        public async Task<IActionResult> WeChatPayNotity()
        {
            try
            {
                //获取支付配置
                var paySetting = await _transactionConfigService.GetDefaultPaySetting();
                var notify = await _client.ExecuteAsync<WeChatPayTransactionsNotify>(_httpContextAccessor.HttpContext?.Request, paySetting.WeChatPay);
                if (notify.TradeState == WeChatPayTradeState.Success)
                {

                    // Console.WriteLine("支付结果通知 => OutTradeNo: "+ notify.OutTradeNo);

                    //await UpdatePayRecordByPayRecordNo(new PayRecordUpdateDto()
                    //{
                    //    PayNo= notify.OutTradeNo,
                    //    TransactionId= notify.TransactionId
                    //});
                    return WeChatPayNotifyResult.Success;
                }

                return WeChatPayNotifyResult.Failure;
            }
            catch (WeChatPayException ex)
            {
                // _logger.LogWarning("出现异常: " + ex.Message);
                Console.WriteLine("支付结果通知_出现异常: " + ex.Message);
                return WeChatPayNotifyResult.Failure;
            }
        }




    }
}
